package com.gray.wow.biz.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gray.wow.biz.ArticleBiz;
import com.gray.wow.biz.ContentBiz;
import com.gray.wow.biz.FamilyBiz;
import com.gray.wow.biz.IndexBiz;
import com.gray.wow.biz.NoticeBiz;
import com.gray.wow.biz.PhotoBiz;
import com.gray.wow.common.DateUtil;
import com.gray.wow.common.Status;
import com.gray.wow.domain.ArticleCategory;
import com.gray.wow.domain.Domain;
import com.gray.wow.domain.Notice;
import com.gray.wow.domain.PageModel;
import com.gray.wow.domain.Photo;
import com.gray.wow.domain.Tag;
import com.gray.wow.freemarker.FreemarkerUtil;

@Service("indexBiz")
@PropertySource("classpath:wow.properties")
@Transactional
public class IndexBizImpl implements IndexBiz {

	@Resource
	private ContentBiz contentBiz;
	@Resource
	private ArticleBiz articleBiz;
	@Resource
	private PhotoBiz photoBiz;
	@Resource
	private NoticeBiz noticeBiz;
	@Resource
	private FamilyBiz familyBiz;

	@Autowired
	private Environment env;
	
	
	@Override
	public Map<String, Object> getFooter() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Domain> linkList = contentBiz.getFooterLink();
		map.put("foot_links", linkList);
		List<Notice> noticeList = noticeBiz.getNotice(2);
		if (noticeList != null && noticeList.size() > 1) {
			map.put("footNotice", noticeList.get(1));
		}

		return map;
	}

	@Override
	public void createFooter(String outpath) {

		outpath += "\\footer.html";
		FreemarkerUtil.getInstance().filePrint("footer.ftl", getFooter(),
				outpath);
	}

	@Override
	public Map<String, Object> getIndexArticle() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ArticleCategory> categoryList = articleBiz.getIndexArticle();
		map.put("categoryList", categoryList);
		return map;
	}

	@Override
	public void createIndexArticle(String outpath) {

		outpath += "\\article.html";
		FreemarkerUtil.getInstance().filePrint("article.ftl",
				getIndexArticle(), outpath);
	}

	@Override
	public Map<String, Object> getIndexPhoto() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Photo> list = photoBiz.getIndexAdvertise();

		if (list != null && list.size() > 4) {
			Iterator<Photo> it = list.iterator();
			int i = 0;
			while (it.hasNext()) {
				Photo widePhoto = it.next();
				if (i == 4) {
					map.put("widePhoto", widePhoto);
					it.remove();
					break;
				}
				i++;
			}
		}

		map.put("adList", list);
		List<Notice> noticeList = noticeBiz.getNotice(2);
		if (noticeList != null && noticeList.size() > 0) {
			map.put("middleNotice", noticeList.get(0));
		}
		map.put("adList", list);
		return map;
	}

	@Override
	public void createIndexPhoto(String outpath) {

		outpath += "\\adv.html";
		FreemarkerUtil.getInstance().filePrint("adv.ftl", getIndexPhoto(),
				outpath);

	}

	@Override
	public void createIndexPage(String outpath) {
		createIndexPhoto(outpath);
		createIndexArticle(outpath);
		createFooter(outpath);

	}

	@Override
	public Map<String, Object> getIndexPage() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Tag> tcList = contentBiz.getTagWidthContent();
		map.put("tcList", tcList);
		PageModel pm5 = contentBiz.getContentByOpenTime(
				DateUtil.getDateStr("yyyy-MM-dd", new Date()), null, 30);// 今日新开
		map.put("pm5", pm5);
		PageModel pm6 = contentBiz.getContentByOpenTime(null,
				DateUtil.getDateStr("yyyy-MM-dd", new Date()), 30);// 三星推荐
		map.put("pm6", pm6);
		PageModel familyPm = familyBiz.getFamily(10, 1,
				"place desc,clicks desc", Status.PASS.getValue());// 家族
		map.put("familyPm", familyPm);
		map.putAll(getFooter());
		map.putAll(getIndexArticle());
		map.putAll(getIndexPhoto());
		return map;
	}
	

	@Override
	public Map<String, String> getMeta() {
		Map<String, String> map=new HashMap<String, String>();
		try {
			map.put("description", new String(env.getProperty("meta.description").getBytes("ISO8859-1"),"utf-8"));
			map.put("keywords", new String(env.getProperty("meta.keywords").getBytes("ISO8859-1"),"utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return map;
	}
}
